package com.example.weight.captcha;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.weight.R;


/**
 * 拼图区域控件
 * Created by judge on 2018/1/17.
 */

public class PictureVerifyView extends LinearLayout {
    //状态码
    private static final int STATE_DOWN = 1;
    private static final int STATE_MOVE = 2;
    private static final int STATE_LOOSEN = 3;
    private static final int STATE_IDEL = 4;
    private static final int STATE_ACCESS = 5;
    private static final int STATE_UNSUCCESS = 6;
    private static final int TOLERANCE = 10;         //验证的最大容差
    private int mState = STATE_IDEL;    //当前状态
    private long startTouchTime;       //滑动/触动开始时间
    private long looseTime;            //滑动/触动松开时间
    private int blockSize = 50;
    private boolean mTouchEnable = true;   //是否可触动
    private Callback callback;
    private ImageView mTotalView;       //整图
    private ImageView mPicView;         //块图

    interface Callback {
        void onSuccess(long time, String vHash, String vCode);

        void onFailed();
    }

    public PictureVerifyView(Context context) {
        this(context, null);
    }

    public PictureVerifyView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public PictureVerifyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initDrawElements();
    }

    private void initDrawElements() {
        View parentView = LayoutInflater.from(getContext()).inflate(R.layout.view_picture_verify, this, true);
        mTotalView = parentView.findViewById(R.id.iv_total_img);
        mPicView = parentView.findViewById(R.id.iv_pic_img);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        reset();
    }

    private void resetData() {
        //图片处理
    }

    /**
     * 按下滑动条(滑动条模式)
     */
    void down(int progress) {
        startTouchTime = System.currentTimeMillis();
        mState = STATE_DOWN;
    }

    /**
     * 触动拼图块(触动模式)
     */
    void downByTouch(float x, float y) {
        mState = STATE_DOWN;
    }

    /**
     * 移动拼图缺块(滑动条模式)
     */
    void move(int progress) {
        mState = STATE_MOVE;
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mPicView.getLayoutParams();
        params.leftMargin = (mTotalView.getWidth() - mPicView.getWidth()) * progress / 100;
        mPicView.setLayoutParams(params);
    }

    /**
     * 触动拼图缺块(触动模式)
     */
    void moveByTouch(float offsetX, float offsetY) {
        mState = STATE_MOVE;
    }

    /**
     * 松开
     */
    void loose(int progress) {
        mState = STATE_LOOSEN;
        looseTime = System.currentTimeMillis();
        checkAccess(progress);
    }


    /**
     * 复位
     */
    void reset() {
        mState = STATE_IDEL;
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mPicView.getLayoutParams();
        params.leftMargin = 0;
        mPicView.setLayoutParams(params);
        resetData();
    }

    void unSuccess() {
        mState = STATE_ACCESS;
        if (callback != null) {
            callback.onFailed();
        }
    }

    void success(String vHash, String vCode) {
        mState = STATE_ACCESS;
        if (callback != null) {
            long deltaTime = looseTime - startTouchTime;
            callback.onSuccess(deltaTime, vHash, vCode);
        }
    }

    void callback(Callback callback) {
        this.callback = callback;
    }

    void setTouchEnable(boolean enable) {
        this.mTouchEnable = enable;
    }


    /**
     * 检测是否通过
     */
    private void checkAccess(int progress) {
        //验证处理
    }
}
